package firstDemo;


import java.io.DataInputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class 微信聊天系统2{
	public static void main(String[] args) throws Exception
	{
		new severs().start();
		new clients().start();
		
	}
}

class clients extends Thread
{
	public void run()
	{
		try {
			Scanner scanner=new Scanner(System.in);
			System.out.println("微信聊天系统已启动");
			DatagramSocket socket=new DatagramSocket();
			while(true) 
			{
			String string=scanner.nextLine();
			if("exit".equals(string)) 
			{
				System.out.println("已退出");
				socket.close();
				break;
			}
			byte[] buffer=string.getBytes();
			DatagramPacket packet=new DatagramPacket
			(buffer, buffer.length, InetAddress.getLocalHost(),4260);
			socket.send(packet);}
			
		} catch (Exception e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
	}
}
class severs extends Thread{
	public void run()
	{
		try {
			DatagramSocket socket=new DatagramSocket(4250);
			byte[] buffer=new byte[64*1024];
			DatagramPacket packet=new DatagramPacket
					(buffer, buffer.length);
			while(true) {
			socket.receive(packet);
			int len=packet.getLength();
			System.out.println("收到"+packet.getAddress().
					getHostAddress()+"--发送的数据--"+new String(buffer,0,len));}
			
		} catch (Exception e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		}
 		
	}
}